CSharpTest.Net
BTreeOrder Property
See Also  Example Send Feedback Download Help File
CSharpTest.Net.BPlusTree Assembly > CSharpTest.Net.Collections Namespace > BPlusTreeOptions<TKey,TValue> Class : BTreeOrder Property

Glossary Item Box

A quick means of setting all the min/max values for the node counts using this value as a basis for the Maximum fields and one-quarter of this value for Minimum fields provided the result is in range.

Syntax

Visual Basic (Declaration) 
Public WriteOnly Property BTreeOrder As Integer
C# 
public int BTreeOrder {set;}

Example

BPlusTree/BPlusTree.Test/TestFileSerialized.cs

C#Copy Code
BPlusTree<int, string>.Options options = (BPlusTree<int, string>.Options)Options;
options.BTreeOrder = 4;
options.FileBlockSize = 512;
options.FileGrowthRate = 25;
options.ConcurrentWriters = 4;
options.FileOpenOptions = FileOptions.None;

using (BPlusTree<int, string> tree = new BPlusTree<int, string>(options))
{
    for(int i=0; i < 100; i++)
        Assert.IsTrue(tree.TryAdd(i, i.ToString()));
}

using (Stream io = TempFile.Open())
{
    //first we can corrupt the root node, which is always at an offset of BlockSize
    io.Seek(512, SeekOrigin.Begin);
    io.Write(new byte[512], 0, 512);

    //Now let's corrupt one byte from the end of the node at index 3
    io.Seek(1024 + 16, SeekOrigin.Begin);
    int len = PrimitiveSerializer.Int32.ReadFrom(io);
    io.Seek(1024 + 32 + len - 1, SeekOrigin.Begin);//secrets of fragmented file revealed... ugly i know.
    io.WriteByte(255); //overwrite last used byte in chunk.
}

options.CreateFile = CreatePolicy.Never;

//Now that we've corrupted part of the file content, let's take a peek
try
{
    using (BPlusTree<int, string> tree = new BPlusTree<int, string>(options))
    {
        foreach (KeyValuePair<int, string> kv in tree)
            Assert.AreEqual(kv.Key.ToString(), kv.Value);
    }
    Assert.Fail("Expected InvalidDataException");
}
catch (InvalidDataException)
{ }

Dictionary<int, string> found = new Dictionary<int, string>();
List<int> duplicates = new List<int>();
foreach (KeyValuePair<int, string> kv in BPlusTree<int, string>.RecoveryScan(options, FileShare.None))
{
    if (!found.ContainsKey(kv.Key))
        found.Add(kv.Key, kv.Value);
    else
        duplicates.Add(kv.Key);
    
    Assert.AreEqual(kv.Key.ToString(), kv.Value);
}
Assert.AreNotEqual(0, found.Count);

//The following may change...
Assert.AreEqual(99, found.Count);
Assert.IsFalse(found.ContainsKey(3), "should be missing #3");
Assert.AreEqual(0, duplicates.Count);
VB.NETCopy Code
Dim options As BPlusTree(Of Integer, String).Options = DirectCast(Options, BPlusTree(Of Integer, String).Options)
options.BTreeOrder = 4
options.FileBlockSize = 512
options.FileGrowthRate = 25
options.ConcurrentWriters = 4
options.FileOpenOptions = FileOptions.None

Using tree As New BPlusTree(Of Integer, String)(options)
    Dim i As Integer = 0
    While i < 100
        Assert.IsTrue(tree.TryAdd(i, i.ToString()))
        System.Math.Max(System.Threading.Interlocked.Increment(i),i - 1)
    End While
End Using

Using io As Stream = TempFile.Open()
    'first we can corrupt the root node, which is always at an offset of BlockSize
    io.Seek(512, SeekOrigin.Begin)
    io.Write(New Byte(512) {}, 0, 512)

    'Now let's corrupt one byte from the end of the node at index 3
    io.Seek(1024 + 16, SeekOrigin.Begin)
    Dim len As Integer = PrimitiveSerializer.Int32.ReadFrom(io)
    io.Seek(1024 + 32 + len - 1, SeekOrigin.Begin)
    'secrets of fragmented file revealed... ugly i know.
        'overwrite last used byte in chunk.
    io.WriteByte(255)
End Using

options.CreateFile = CreatePolicy.Never

'Now that we've corrupted part of the file content, let's take a peek
Try
    Using tree As New BPlusTree(Of Integer, String)(options)
        For Each kv As KeyValuePair(Of Integer, String) In tree
            Assert.AreEqual(kv.Key.ToString(), kv.Value)
        Next
    End Using
    Assert.Fail("Expected InvalidDataException")
Catch generatedExceptionName As InvalidDataException
End Try

Dim found As New Dictionary(Of Integer, String)()
Dim duplicates As New List(Of Integer)()
For Each kv As KeyValuePair(Of Integer, String) In BPlusTree(Of Integer, String).RecoveryScan(options, FileShare.None)
    If Not found.ContainsKey(kv.Key) Then
        found.Add(kv.Key, kv.Value)
    Else
        duplicates.Add(kv.Key)
    End If

    Assert.AreEqual(kv.Key.ToString(), kv.Value)
Next
Assert.AreNotEqual(0, found.Count)

'The following may change...
Assert.AreEqual(99, found.Count)
Assert.IsFalse(found.ContainsKey(3), "should be missing #3")
Assert.AreEqual(0, duplicates.Count)

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys